<?
	require_once "DB.php";	
	require_once "../../commons/db/Recordset.cls";	
	
	$cacheDb["id"] = "";
	function db($connId=""){
		global $cacheDb;
	 	$accessStr = 'odbc(access)://admin@';
		$connections["Listini"]  ="$accessStr /piv_listini";
		$connections["Listinib"]  ="$accessStr /piv_listinib";
		$connections["Trattamenti"]  ="$accessStr /piv_trattamenti";
		$connections["Trattamentib"]  ="$accessStr /piv_trattamentib";
		$connections["Controllo"]  ="$accessStr /piv_contrges";
		$connections["Maga"]  ="$accessStr /piv_magazzino"; 
		$connections["Mov"]  ="$accessStr /piv_movim";
		$connections["Movb"]  ="$accessStr /piv_movimb";
		
		if ($connId==""){
			$connId	= "Trattamenti";
		}  
		
		$db = & $cacheDb[$connId] ; 
		
		
		
		if( isset($db) ) {
			return $db;
		}
		 
		$connstring = $connections[$connId] ; 
		$db = & DB::connect($connstring);
		
		$cacheDb[$connId] = $db;
		 
		
		return $db;
	}	
	
	function rs($sql, $parameters="", $connId="",$immediate=true, $startLimit=0,$endLimit=0){
		
		$db = db($connId); 
		$rs=new Recordset($db,$sql, $parameters, $immediate, $startLimit,$endLimit);
		
		return $rs;
	}
	
	function sqlExecute($sql,$connId=""){
		if($sql == "") return FALSE;
		$db = db($connId);
		$res = $db->query($sql);
		if (get_class($res)=='db_error') {
			if ($GLOBALS['debugger']=='on') {
			    echo "<pre>";print_r($res);exit();
			}
		    
		}
		return $res;
	}
	
	// last identity
	function lastIdentity($connId=""){
		$db = db($connId);
		$sql = "SELECT @@IDENTITY AS 'Identity'";
		$id=fLookUp($sql, 'Identity');
		return $id;
	}

	// return a recordset as matrix
	function aLookUp($sql){
		$rs = rs($sql);
		if($rs->recordCount>0){
			while($r=$rs->getRow()){
				$a[]=$r;
			}
			return $a;
		}
	}

	// return a recordset as matrix with index based on key field
	function aKeyLookUp($sql, $keyField){
		$rs = rs($sql);
		if($rs->recordCount>0){
			while($r=$rs->getRow()){
				$a[$r[$keyField]]=$r;
			}
			return $a;
		}
	}
	
	// return a recordset as fields array
	function rLookUp($sql){
		$rs = rs($sql);
		if($rs->recordCount>0){
			return $rs->getRow();
		}
	}
	
	// return the value of fd
	function fLookUp($sql, $fd){
		$r = rLookUp($sql);
		return $r[$fd];
	}

	// return a recordset as matrix
	function lLookUp($sql, $fd){
		$rs = rs($sql);
		if($rs->recordCount>0){
			while($r=$rs->getRow()){
				$a[]=$r[$fd];
			}
			return $a;
		}
	}
	
	function connTest(){
		$sql = "SELECT * FROM Dim_SalesPerson ";
		$rs = rs($sql, false,"", true, 0,20);
		
		if($rs->recordCount>0){
			echo "<pre>";
			while ($values = $rs->getRow()){
				print_r($values);
			}
			echo "</pre>";
		}
	}
	
	// save a record change/update into log
	function logRecord($LogTable, $LogAction, $recId=''){
		if (empty($recId)) {
		 	$recId = lastIdentity();
		}
		
		$LogDate = date("YmdGis");
		$LogUser = $_SESSION['UserId'];
		$req = var_export($_REQUEST, TRUE);
		
		$sql = "INSERT INTO LogTable(LogTable, LogTableID, LogDate, LogUser, LogAction, LogRequest) VALUES("
		     . quoteValue($LogTable, "text"). ", "
		     . "$recId, "
		     . quoteValue($LogDate, "text"). ", "
		     . quoteValue($LogUser, "text"). ", "
		     . quoteValue($LogAction, "text"). ", "
		     . quoteValue($req, "text")
			 . ")";

		$res = &sqlExecute($sql);
	}

	// save a record Event for log history
	function logEvent($LogTable, $LogAction, $recId){

		$LogDate = date("YmdGis");
		$LogUser = $_SESSION['UserId'];
		
		$sql = "INSERT INTO LogTable(LogTable, LogTableID, LogDate, LogUser, LogAction) VALUES("
		     . quoteValue($LogTable, "text"). ", "
		     . "$recId, "
		     . quoteValue($LogDate, "text"). ", "
		     . quoteValue($LogUser, "text"). ", "
		     . quoteValue($LogAction, "text")
			 . ")";

		$res = &sqlExecute($sql);
	}
	
?>